package com.northone.statistics.fragment;

import com.northone.statistics.BaseFragment;
import com.northone.statistics.R;
import com.northone.statistics.database.SaintDataBaseHelper;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

public class HomeFragment extends BaseFragment {
	private View view;

	@SuppressLint("InflateParams")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_home, null);
		initTitleBar();
		initView();
		return view;
	}

	private void initTitleBar() {
		TextView title = (TextView) view.findViewById(R.id.tv_title);
		title.setText(R.string.home);
	}

	private void initView() {
		final TextView tv_text = (TextView) view.findViewById(R.id.tv_text);
		TextView totalNumbers = (TextView) view.findViewById(R.id.tv_totalNumbers);
		TextView totalBaptism = (TextView) view.findViewById(R.id.tv_totalBaptism);
		
		tv_text.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(activity);
				builder.setTitle("更改圣经节");
				final EditText txtContent = new EditText(activity);
				builder.setView(txtContent)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						tv_text.setText(txtContent.getText().toString());
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				builder.create().show();
			}
		});

		int baptismNumbers = 0;

		Cursor saint_Cursor = application.database.query(SaintDataBaseHelper.TABLE_NAME_BASE, null, null, null, null, null, null);
		Cursor month_Cursor = application.database.query(SaintDataBaseHelper.TABLE_NAME_MONTH_ITEM, null, null, null, null, null,
				null);
		boolean flag = month_Cursor.moveToFirst();
		while (flag) {
			String baptismNumStr = month_Cursor.getString(month_Cursor.getColumnIndex("baptismNum"));
			baptismNumStr = baptismNumStr.substring(baptismNumStr.indexOf("浸") + 1, baptismNumStr.indexOf("人"));
			int month_baptism = Integer.parseInt(baptismNumStr);
			baptismNumbers = month_baptism + baptismNumbers;
			flag = month_Cursor.moveToNext();
		}
		totalNumbers.setText("总人数:" + saint_Cursor.getCount());
		totalBaptism.setText("累计受浸人数:" + baptismNumbers);
		tv_text.setText("耶和华阿，愿你的仇敌都这样灭亡。愿爱你的人如日头出现，光辉烈烈 ....  士师记 5:31");
		saint_Cursor.close();
		month_Cursor.close();
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
	}
}
